import { mapGetters } from 'vuex';
import { alertMessage, isValidateNull } from '@/utils/utils';
import { getMenuDetailApi, createOrEditMenuApi } from './create-menu.service';
export default {
  name: 'create-menu',
  components: {},
  directives: {},
  props: {
    type: {
      type: String,
      default: ''
    },
    menus: {
      type: Object,
      default: () => null
    },
    activeNav: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false, // table loading
      showDialog: true, // 是否显示弹窗
      title: '创建菜单',
      editForm: {}, // 编辑用户表单
      editRules: {
        name: [{ required: true, trigger: "change", validator: isValidateNull }],
        code: [{ required: true, trigger: "change", validator: isValidateNull }],
        path: [{ required: true, trigger: "change", validator: isValidateNull }]
      } // 编辑表单验证规则
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    // 创建菜单
    if (this.type === 'createTop') {
      this.title = '创建菜单';
      this.$set(this.editForm, 'pname', '根目录');
      this.$set(this.editForm, 'pid', 0);
    }
    // 创建子部门
    if (this.type === 'create') {
      this.title = '创建菜单';
      this.$set(this.editForm, 'pname', this.menus.name);
      this.$set(this.editForm, 'pid', this.menus.objectId);
      this.$set(this.editForm, 'isShow', 1);
      this.$set(this.editForm, 'isAuthenticate', 1);
    }
    // 编辑当前部门
    if (this.type === 'edit') {
      this.title = '编辑菜单';
      this.getMenuDetail();
    }
  },
  methods: {
    // 获取菜单详情
    getMenuDetail() {
      getMenuDetailApi(this.menus.objectId).then(res => {
        this.editForm = res.data
      })
    },

    // 创建或者编辑信息
    save() {
      // 表单验证
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.editForm.applicationId = this.activeNav;
          createOrEditMenuApi(this.editForm).then((res) => {
            alertMessage(res.message);
            this.showDialog = false;
            this.$emit('reload', true)
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        }
      });
    },

    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.showDialog = false;
      this.$emit('close', true);
    }
  }
}
